package main

import (
	"fmt"
	"html/template"
	"io/ioutil"
	"os"

	"github.com/tealeg/xlsx"
)

type Card struct {
	Name         string
	CardNum      string
	School       string
	Grade        string
	Class        string
	Group        string
	RoomNum      string
	SiteNum      string
	RoomName     string
	RoomPosition string
	Date         string
	Tel          string
}

func main() {
	//打开excel文件
	excel, err := xlsx.OpenFile("/Users/hyh/Downloads/data1.xlsx")
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	var sheets = excel.Sheets
	var rows = sheets[0].Rows
	data := []Card{}
	content, _ := ioutil.ReadFile("//Users/hyh/go-work/src/readexcel/index.html")
	tmpl, _ := template.New("test").Parse(string(content))
	page := 1
	for index, row := range rows {
		cells := row.Cells
		data = append(data, Card{
			Name:         cells[0].String(),
			CardNum:      cells[1].String(),
			School:       cells[2].String(),
			Grade:        cells[3].String(),
			Class:        cells[4].String(),
			Group:        cells[5].String(),
			RoomNum:      cells[6].String(),
			SiteNum:      cells[7].String(),
			RoomName:     cells[8].String(),
			RoomPosition: cells[9].String(),
			Date:         cells[10].String(),
			Tel:          cells[11].String(),
		})
		if (index+1)%6 == 0 {
			f, _ := os.Create(fmt.Sprintf("/Users/hyh/go-work/src/readexcel/result/data%d.html", page))
			tmpl.Execute(f, data)
			f.Close()
			page = page + 1
			data = []Card{}
		}
	}
	f, _ := os.Create(fmt.Sprintf("/Users/hyh/go-work/src/readexcel/result/data%d.html", page))
	tmpl.Execute(f, data)
	f.Close()
}
